package curd

import (
	"github.com/gin-gonic/gin"
	// "go.uber.org/zap"
	// "goskeleton/app/global/variable"
	
	"goskeleton/app/model/employee"
	"goskeleton/app/service/auth_system_menu"
)

func CreateEmployeeCurdFactory() *EmployeeCurd {
	return &EmployeeCurd{EmployeeModel: employee.CreateEmployeeFactory("")}
}

type EmployeeCurd struct {
	EmployeeModel *employee.EmployeeModel
}

func (e *EmployeeCurd) InsertData(c *gin.Context) bool {
	return e.EmployeeModel.InsertData(c)

}

func (e *EmployeeCurd) UpdateData(c *gin.Context) bool {
	return e.EmployeeModel.UpdateData(c)
}

func (e *EmployeeCurd) ListData(
	limits, limitItems int, order string,
	name string, workStatus int, contractType int,
	hireStartDate string, hireEndDate string,
	departmentId int,
	positionId int,
) (int64, []*employee.EmployeeModel) {
	var departmentIds []int

	if positionId > 0 {

	} else if departmentId > 0 {
		departmentSer := &auth_system_menu.AuthSystemMenuService{}
		departmentIds = departmentSer.GetAllChildIdsById(departmentId, 1)

	}

	totalCounts, tmp := e.EmployeeModel.ListData(
		limits, limitItems, order, name, workStatus, contractType, hireStartDate, hireEndDate, departmentIds, positionId)
	if totalCounts > 0 {
		OrganizationFactory := &auth_system_menu.AuthSystemMenuService{}
		for _, item := range tmp {
			if item.PositionId > 0 {
				item.PositionName = OrganizationFactory.GetFindById(item.PositionId).Title
			}

			if item.DepartmentId > 0 {
				item.DepartmentName = OrganizationFactory.GetFindById(item.DepartmentId).Title
			}

		}
		return totalCounts, tmp
	}
	return 0, nil
}

func (e *EmployeeCurd) ShowOneItem(id int) (tmp *employee.EmployeeModel) {
	tmp = e.EmployeeModel.ShowOneItem(id)
	if tmp != nil {
		OrganizationFactory := &auth_system_menu.AuthSystemMenuService{}
		if tmp.PositionId > 0 {
			tmp.PositionName = OrganizationFactory.GetFindById(tmp.PositionId).Title
		}

		if tmp.DepartmentId > 0 {
			tmp.DepartmentName = OrganizationFactory.GetFindById(tmp.DepartmentId).Title
		}

		return tmp
	}
	return nil
}

func (e *EmployeeCurd) GetEmployeeByIds(ids []int) (tmp []*employee.EmployeeModel) {
	return e.EmployeeModel.GetEmployeeByIds(ids)
}

func (e *EmployeeCurd) GetTotalStatistcis() (totalStatistics *employee.TotalStatistics) {
	totalStatistics = &employee.TotalStatistics{}
	var tmpPositionStatistics []employee.TotalPositionStatistics
	var tmpPositionIds []int
	// 添加contractStats非空检查，增强代码健壮性
	contractStats := e.EmployeeModel.GetTotalContractTypeStatistics()
	if contractStats != nil {
		totalStatistics.TotalContractTypeStatistics = *contractStats
	}
	// 转换指针切片为非指针切片
	positionStatsPtr := e.EmployeeModel.GetTotalPositionStatistics()
	tmpPositionStatistics = make([]employee.TotalPositionStatistics, len(positionStatsPtr))
	for i, ptr := range positionStatsPtr {
		if ptr != nil {
			tmpPositionStatistics[i] = *ptr
			tmpPositionIds = append(tmpPositionIds, ptr.PositionId)
		}
	}
	// 获取岗位名称
	orgPostFactory := &auth_system_menu.AuthSystemMenuService{}
	orgPostList := orgPostFactory.GetAllPostsIdAndTitleByIds(tmpPositionIds)
	// 构建岗位名称映射
	positionNameMap := make(map[int]string)
	for _, post := range orgPostList {
		positionNameMap[post.Id] = post.Title
	}
	// 填充岗位名称
	for i := range tmpPositionStatistics {
		tmpPositionStatistics[i].PositionName = positionNameMap[tmpPositionStatistics[i].PositionId]
	}
	// 将非指针切片转换为指针切片
	ptrSlice := make([]*employee.TotalPositionStatistics, len(tmpPositionStatistics))
	for i := range tmpPositionStatistics {
		ptrSlice[i] = &tmpPositionStatistics[i]
	}
	totalStatistics.TotalPositionStatistics = ptrSlice

	return totalStatistics
}

func (e *EmployeeCurd) GetAllIdNameByWhere(departmentId int, positionId int) (totalCounts int64, tmp []*employee.IdAndName) {
	// if departmentId == 0 && positionId == 0 {
	// 	return 0, nil
	// }
	var departmentIdInts []int
	if positionId > 0 {

		return e.EmployeeModel.GetAllIdNameByWhere([]int{}, positionId)
	} else if departmentId > 0 {
		departmentIds := (&auth_system_menu.AuthSystemMenuService{}).GetAllChildIdsById(departmentId, 1)

		for _, item := range departmentIds {
			departmentIdInts = append(departmentIdInts, int(item))
		}

		return e.EmployeeModel.GetAllIdNameByWhere(departmentIdInts, 0)
	} else {
		return e.EmployeeModel.GetAllIdNameByWhere([]int{}, 0)
	}

	// return 0, nil

}

func (e *EmployeeCurd) GetOneDataById(id int) (tmp *employee.EmployeeModel) {
	return e.EmployeeModel.ShowOneItem(id)
}

func (e *EmployeeCurd) DeleteData(id int) bool {
	return e.EmployeeModel.DeleteData(id)
}
